package com.zy.web.controller.bs;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zy.entity.bs.Car;
import com.zy.pojo.query.bs.CarQuery;
import com.zy.pojo.vos.common.PageVo;
import com.zy.pojo.vos.common.ResultVo;
import com.zy.service.bs.CarService;
import com.zy.web.controller.base.BaseController;
import com.zy.web.utils.ResultUtil;

import java.io.Serializable;

@RequestMapping({"bs/car","m/bs/car"})
@Controller
public class CarController extends BaseController {

	@Autowired
	private CarService carService;

	/**
	 * 分页
 	 * 
 	 * @param carQuery
	 * @param start
	 * @param limit
	 * @return
	 */
	@RequestMapping("pageCar")
	@ResponseBody
	public PageVo pageCar(CarQuery carQuery, Integer start, Integer limit) {
			return this.carService.pageCar(carQuery, start, limit);
//		Car dealer

	}

	/**
	 * 新增
	 * 
	 * @param car
	 * @return
	 */
	@RequestMapping(value = "saveCar", method = RequestMethod.POST)
	@ResponseBody
	public ResultVo saveCar(Car car) {
		try {
			car.setCreateUserId(this.getCurrentUser().getId());
			car.setModifyUserId(this.getCurrentUser().getId());
			this.carService.saveCar(car);
			return ResultUtil.success();
		} catch (Exception e) {
			return ResultUtil.error(log, e);
		}
	}
	
	/**
	 * 更新
	 * 
	 * @param car
	 * @return
	 */
	@RequestMapping(value = "updateCar", method = RequestMethod.POST)
	@ResponseBody
	public ResultVo updateCar(Car car) {
		try {
			car.setModifyUserId(this.getCurrentUser().getId());
			this.carService.updateCar(car);
			return ResultUtil.success();
		} catch (Exception e) {
			return ResultUtil.error(log, e);
		}
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "removeCar", method = RequestMethod.POST)
	@ResponseBody
	public ResultVo removeCar(long id) {
		try {
			this.carService.removeCar(id);
			return ResultUtil.success();
		} catch (Exception e) {
			return ResultUtil.error(log, e);
		}
	}

	/**
	 * 恢复
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "recover", method = RequestMethod.POST)
	@ResponseBody
	public ResultVo recover(long id) {
		try {
			this.carService.recover(id);
			return ResultUtil.success();
		} catch (Exception e) {
			return ResultUtil.error(log, e);
		}
	}

	@RequestMapping(value = "getListCars")
	@ResponseBody
	public ResultVo getListCars() {
		try {
			return ResultUtil.success((Serializable) carService.getListCars(this.getCurrentUser().getId()));
		} catch (Exception e) {
			return ResultUtil.error(log, e);
		}
	}

	@RequestMapping(value = "addCar", method = RequestMethod.POST)
	@ResponseBody
	public ResultVo addCar(Car car) {
		try {
			car.setModifyUserId(this.getCurrentUser().getId());
			this.carService.saveCar(car);
			return ResultUtil.success();
		} catch (Exception e) {
			return ResultUtil.error(log, e);
		}
	}

    @RequestMapping(value = "appUpdateCar", method = RequestMethod.POST)
    @ResponseBody
    public ResultVo update(Car car) {
        try {
            this.carService.updateCar(car);
            return ResultUtil.success();
        } catch (Exception e) {
            return ResultUtil.error(log, e);
        }
    }

}